package com.workcenter.app;

import java.security.PublicKey;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.ppcodes.core.data.DXml;
import com.ppcodes.core.data.DXml.FieldList;
import com.ppcodes.core.utils.UtiDialogsFactory;
import com.workcenter.base.UIBase;
import com.workcenter.controls.AdpLV_CustomerInfo;
import com.workcenter.controls.AdpLV_RepairsInfo;
import com.workcenter.entity.NetClient;
import com.workcenter.entity.PubConst;
import com.workcenter.entity.Session;
import com.workcenter.entity.UrlParams;
import com.workcenter.entity.model.ECustomerInfo;
import com.workcenter.entity.model.ERepairsInfo;

public class UICustomerList extends UIBase
{
   private ListView listView;
   private EditText edtCustomerName;
   private EditText edtInstrument;
   private Button btnSearch;

   private void InitControls()
   {
	  listView = (ListView) findViewById(R.id.list_customerlist);
	  listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	  {
		 @Override
		 public void onItemClick(AdapterView<?> arg0, View currentView, int position, long arg3)
		 {
			Intent intent=new Intent();
			intent.putExtra(PubConst.Intent_Key.CustomerInfo, (ECustomerInfo)listView.getAdapter().getItem(position));
            setResult(RESULT_OK, intent);
            
            finish();
		 }
	  });
	  edtCustomerName = (EditText) findViewById(R.id.edtCustomerName_customerlist);
	  edtInstrument = (EditText) findViewById(R.id.edtInstrumentModel_customerlist);
	  btnSearch = (Button) findViewById(R.id.btnSearch_customerlist);
	  btnSearch.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			if (edtCustomerName.getText().toString().trim().equals("") && edtInstrument.getText().toString().trim().equals(""))
			{
			   UtiDialogsFactory.getInstance().ShowToastInfoShort(UICustomerList.this, getString(R.string.newrepair_nameinstrument_noempty));
			   return;
			}
			String name = edtCustomerName.getText() == null || edtCustomerName.getText().toString().trim().equals("") ? "" : edtCustomerName.getText().toString().trim();
			String instru = edtInstrument.getText() == null || edtInstrument.getText().toString().trim().equals("") ? "" : edtInstrument.getText().toString().trim();

			executeAsyncTask(PubConst.TaskName.SearchCustomer, new Object[] { name, instru });

		 }
	  });
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.customerlist);
	  InitControls();

   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UICustomerList.this, getString(R.string.dialog_searching));
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  if (taskName != null && taskName.equals(PubConst.TaskName.SearchCustomer))
	  {
		 try
		 {
			return NetClient.SearchCustomers(String.valueOf(params[0]), String.valueOf(params[1]));
		 }
		 catch (Exception e)
		 {
			if (Session.getInstance().IsDEBUG())
			{
			   Log.e("ERROR", "UICustomerList_onAsyncTaskInBackground(taskName, params) ", e);
			}
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  try
	  {
		 UtiDialogsFactory.getInstance().DismissLoadingDialog();
		 if (result != null)
		 {
			DXml data = (DXml) result;
			if (data.getHead().GetFieldValue("RspCode").equals(UrlParams.SuccessCode))
			{
			   ArrayList<ECustomerInfo> list = new ArrayList<ECustomerInfo>();
			   ECustomerInfo rInfo = null;
			   int size = data.getBody().getChilds().length;
			   for (int i = 0; i < size; i++)
			   {
				  FieldList fieldList = data.getBody().GetFieldList(i);

				  rInfo = new ECustomerInfo();
				  rInfo.setContact(fieldList.GetFieldValue("Contact"));
				  rInfo.setCustomerName(fieldList.GetFieldValue("CustomerName"));
				  rInfo.setInstrument(fieldList.GetFieldValue("Instrument"));
				  rInfo.setInstrumentID(fieldList.GetFieldValue("InstrumentID"));
				  rInfo.setInstrumentState(fieldList.GetFieldValue("InstrumentState"));
				  rInfo.setPhone(fieldList.GetFieldValue("Phone"));
				  rInfo.setRoomName(fieldList.GetFieldValue("RoomName"));
				  rInfo.setArea(fieldList.GetFieldValue("Area"));

				  list.add(rInfo);
			   }

			   AdpLV_CustomerInfo adp = new AdpLV_CustomerInfo(list, UICustomerList.this);
			   listView.setAdapter(adp);
			}
			else
			{
			   UtiDialogsFactory.getInstance().ShowToastInfoShort(UICustomerList.this, data.getHead().GetFieldValue("RspMsg"));
			}
		 }
		 else
		 {
			UtiDialogsFactory.getInstance().ShowToastInfoShort(UICustomerList.this, getString(R.string.dialog_server_failed));
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UIMain_GetListData(result) ", e);
		 }
	  }
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if(keyCode==event.KEYCODE_BACK)
	  {
		 setResult(RESULT_CANCELED);
		 finish();
	  }
	  return false;
   }

}
